package com.wx.mythread.stopthread;

/**
 * 最佳操作：
 * 在try/catch捕获InterruptedException异常后，应该讲异常抛出来，在方法上
 * 声明异常，那么run方法就需要强制进行try/catch操作
 */
public class RightWayStopThreadWithInProd implements Runnable {
    @Override
    public void run() {
        while (true && !Thread.currentThread().isInterrupted()){
            System.out.println("go");
            try {
                throwInMethod();
            } catch (InterruptedException e) {
                System.out.println("保存日志");
                e.printStackTrace();
            }
        }
    }

    private void throwInMethod() throws InterruptedException {
        // try {
        //     Thread.sleep(2000);
        // } catch (InterruptedException e) {
        //     e.printStackTrace();
        // }
        Thread.sleep(2000);
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithInProd());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }
}
